package com.simulated.communication.ossd;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @Author Markburt
 * @Description :JDK 11 跨域
 * @DATE: 2020/5/2
 * @TIME: 下午 04:56
 * @YEAR: 2020
 * @Version :
 **/
@SuppressWarnings("all")
public class DisableAccessWarnings {

    /**
     * JDK 跨域 非法反射
     */
    @SuppressWarnings("unchecked")
    public static void disableAccessWarnings() {
        try {
            Class unsafeClass = Class.forName("sun.misc.Unsafe");
            Field field = unsafeClass.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            Object unsafe = field.get(null);

            Method putObjectVolatile = unsafeClass.getDeclaredMethod("putObjectVolatile", Object.class, long.class, Object.class);
            Method staticFieldOffset = unsafeClass.getDeclaredMethod("staticFieldOffset", Field.class);

            Class loggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
            Field loggerField = loggerClass.getDeclaredField("logger");
            Long offset = (Long) staticFieldOffset.invoke(unsafe, loggerField);
            putObjectVolatile.invoke(unsafe, loggerClass, offset, null);
        } catch (Exception ignored) {}
    }




}
